<template>
  <div class="background">
    <a-card style="margin-bottom: 10px">
      <a-row>
        <a-col :span="24">
          <a-form
            layout="inline"
            :form="form"
            @submit="handleSubmit"
          >
            <a-form-item label="图文标题">
              <a-input
                allow-clear
                placeholder="请输入图文标题"
                v-model="title"
              ></a-input>
            </a-form-item>
            <a-form-item>
              <a-button
                type="primary"
                html-type="submit"
              >搜索</a-button>
            </a-form-item>
            <a-form-item>
              <a-button
                ghost
                type="primary"
                @click="sync"
              >同步图文发布记录</a-button>
            </a-form-item>
          </a-form>
        </a-col>
      </a-row>
    </a-card>
    <a-card>
      <a-menu
        v-model="current"
        mode="horizontal"
        @click="handleClick"
      >
        <a-menu-item key="1">图文列表 </a-menu-item>
        <a-menu-item key="2">发布记录 </a-menu-item>
      </a-menu>
      <div id="change">
        <a-table
          bordered
          :loading="loading"
          :pagination="pagination"
          :columns="columns"
          :data-source="dataSource"
          @change="handleTableChange"
          :scroll="{ x: true }"
        >
          <template
            slot="type"
            slot-scope="txt, record"
          >
            <span
              v-for="item in typeList"
              :key="item.id"
            >
              <span v-if="item.id == record.type">{{item.name}}</span>
            </span>
          </template>
          <template
            slot="action"
            slot-scope="txt, record"
          >
            <a
              class="mr-10"
              @click="copy(record.articleId)"
            >
              复制ArticleID
            </a>
            <a
              class="mr-10"
              @click="toDetail(record)"
            >
              详情
            </a>

            <!-- <a
              class="mr-10"
              @click="deleteRecord(record)"
            >
              删除
            </a> -->
          </template>
        </a-table>
      </div>
      <detail
        v-if="showDetail"
        :detail="detail"
        @close="close"
      ></detail>

    </a-card>

  </div>
</template>

<script>
import { postAction, getAction } from '@/api/manage'
import detail from './detail.vue'
const typeList = [
  {
    id: 0,
    name: '草稿箱'
  },
  {
    id: 1,
    name: '已发布'
  },
  {
    id: 2,
    name: '图文群发'
  }
]
const columns = [
  {
    title: '序号',
    width: 70,
    dataIndex: 'id'
  },
  {
    title: '图文标题',
    dataIndex: 'title',
    width: 140
  },
  {
    title: '图文发布类型',
    width: 140,
    scopedSlots: { customRender: 'type' }
  },
  {
    title: '作者',
    dataIndex: 'author',
    width: 100
  },
  {
    title: '封面素材ID',
    dataIndex: 'thumbMediaId'
  },
  {
    title: '创建时间',
    width: 200,
    dataIndex: 'updateTime'
  },
  {
    title: '操作',
    width: 170,
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  components: {
    detail
  },
  props: {
    appid: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      columns,
      current: ['1'],
      typeList,
      urlKey: 'basemgt-wx',
      dataSource: [],
      loading: false,
      title: '',
      form: this.$form.createForm(this),
      showDetail: false,
      detail: {},
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: total => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        }
      }
    }
  },
  mounted() {
    this.loadData()
  },
  watch: {
    appid() {
      this.loadData()
    }
  },
  methods: {
    handleTableChange(pagination) {
      this.pagination = pagination
      this.loadData()
    },
    close() {
      this.showDetail = false
      this.loadData()
    },
    toDetail(record) {
      this.showDetail = true
      this.detail = record
    },
    changeMenu() {
      this.loadData()
    },
    handleClick(e) {
      console.log('click', e.key)
      this.type = e.key
      this.pageNum = 1
      this.loadData(1)
    },
    handleSubmit(e) {
      e.preventDefault()
      this.loadData(1)
    },
    loadData(key) {
      this.loading = true
      const params = {
        appid: this.appid,
        pageSize: this.pagination.pageSize,
        pageNum: this.pagination.current
      }
      if (key) {
        this.pagination.current = 1
        params.title = this.title
        params.pageNum = 1
      }
      postAction(`${this.urlKey}/wxGraphic/findWxGraphicListByCondition`, params).then(data => {
        if (data.code == 200) {
          this.dataSource = data.data.records
          this.pagination.total = data.data.total
        } else {
          this.$message.warning(data.msg || data.message)
        }
      })
      this.loading = false
    },
    sync() {
      const params = {
        appid: this.appid
      }
      getAction(`${this.urlKey}/wxGraphic/getSuccessReleaseListData`, params).then(data => {
        if (data.code == 200) {
          this.$message.success('同步成功！')
        } else {
          this.$message.warning('同步失败！')
        }
      })
    },
    copy(articleId) {
      if (articleId) {
        var cInput = document.createElement('input')
        cInput.value = articleId
        document.body.appendChild(cInput)
        cInput.select() // 选取文本框内容
        document.execCommand('copy')
        this.$message.success('articleId复制成功')
        // 复制成功后再将构造的标签 移除
        document.body.removeChild(cInput)
      } else {
        this.$message.error('该图文不存在articleId！')
      }
    }
  }
}
</script>
<style scoped>
.start::before {
  content: ' ';
  border: 4px solid red;
  border-radius: 4px;
  position: absolute;
  margin-left: -9px;
  margin-top: 6px;
}
.end::before {
  content: ' ';
  border: 4px solid green;
  border-radius: 4px;
  position: absolute;
  margin-left: -9px;
  margin-top: 6px;
}
</style>

<style>
#change .ant-table {
  font-size: 15px !important;
}
</style>
